/**
 * SysDynamicServiceImpl.java -- 2017-10-23 17:14:31 作者：欧增奇 -- 代码生成器自动生成 联系方式：425752797@qq.com http://www.ouzengqi.com/
 */
package com.ozq.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ozq.common.page.Page;
import com.ozq.common.page.PageUtil;
import com.ozq.common.util.CommonUtils;
import com.ozq.common.util.UuidUtils;
import com.ozq.sys.dao.entity.SysDynamic;
import com.ozq.sys.dao.entity.SysText;
import com.ozq.sys.dao.mapper.SysDynamicMapper;
import com.ozq.sys.dao.mapper.SysTextMapper;
import com.ozq.sys.service.inter.SysDynamicService;

/**
 * <动态表接口实现> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017-10-23 17:14:31]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service("sysDynamicService")
public class SysDynamicServiceImpl implements SysDynamicService
{
    static Logger logger = LoggerFactory.getLogger(SysDynamicServiceImpl.class);

    /**
     * 动态表Mapper[SysDynamic]
     */
    @Autowired
    private SysDynamicMapper sysDynamicMapper;

    /**
     * 系统-文本表Mapper[SysText]
     */
    @Autowired
    private SysTextMapper sysTextMapper;

    /** ------------------------------------公共-工具------------------------------------ **/
    /**
     * 分页公共实现
     */
    @Override
    public Page pageManage(Page page)
    {
        logger.debug("debug page: {}", page);

        // 默认每页10条记录
        if (page.getEveryPage() <= 0)
            page.setEveryPage(10);

        // 默认第一页
        if (page.getCurrentPage() <= 0)
            page.setCurrentPage(1);

        return PageUtil.createPage(page.getEveryPage(), page.getTotalCount(), page.getCurrentPage());
    }

    /**
     * 缓存清理
     */
    @Override
    public void clearCache()
    {
        logger.debug("清理动态表缓存成功:{}", "sysDynamic");
    }

    /** ------------------------------------公共-业务------------------------------------ **/
    /** --公共-业务-统计-- **/
    /**
     * 统计动态表所有的记录数
     * 
     * @return int
     */
    @Override
    public int countAll()
    {
        return sysDynamicMapper.countAll();
    }

    /**
     * 按照条件精确统计动态表
     * 
     * @param sysDynamic
     * @return int
     */
    @Override
    public int countBy(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return 0;

        return sysDynamicMapper.countBy(sysDynamic);
    }

    /**
     * 按照条件模糊统计动态表
     * 
     * @param sysDynamic
     * @return int
     */
    @Override
    public int countLike(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return 0;

        return sysDynamicMapper.countLike(sysDynamic);
    }

    /** --公共-业务-添加-- **/
    /**
     * 插入动态表
     * 
     * @param sysDynamic
     * @return sysDynamic
     */
    @Override
    public SysDynamic add(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return null;

        // 过滤数据
        sysDynamic.setTextCode(CommonUtils.filterScript(sysDynamic.getTextCode()));

        // 插入内容
        SysText sysText = new SysText();
        sysText.setTextCode(UuidUtils.shortUuid());
        sysText.setTextContent(sysDynamic.getTextCode());
        sysTextMapper.insert(sysText);

        sysDynamic.setTextCode(sysText.getTextCode());
        sysDynamicMapper.insert(sysDynamic);
        return sysDynamic;
    }

    /**
     * 插入动态表集合
     * 
     * @param sysDynamic
     * @return boolean
     */
    @Override
    public boolean addList(List<SysDynamic> sysDynamics)
    {
        logger.debug("debug sysDynamics: {}", sysDynamics);

        if (sysDynamics == null || sysDynamics.isEmpty())
            return false;

        List<SysDynamic> inserts = filterEmpty(sysDynamics);
        if (inserts.isEmpty())
            return false;

        sysDynamicMapper.insertList(inserts);
        return true;
    }

    /**
     * 批量插入动态表，当id存在更新这条数据，不存在插入这条数据（注意：会影响所有字段）
     * 
     * @param sysDynamics
     * @return boolean
     */
    @Override
    public boolean addUpdateList(List<SysDynamic> sysDynamics)
    {
        logger.debug("debug sysDynamics: {}", sysDynamics);

        if (sysDynamics == null || sysDynamics.isEmpty())
            return false;

        List<SysDynamic> inserts = filterEmpty(sysDynamics);
        if (inserts.isEmpty())
            return false;

        sysDynamicMapper.insertUpdateList(inserts);
        return true;
    }

    /** --公共-业务-查询-- **/
    /**
     * 按照动态表查询,如果存在id，则用id查询(本方法只会返回一条数据，当查询有1条以上返回null)
     * 
     * @param sysDynamic
     * @return sysDynamic
     */
    @Override
    public SysDynamic find(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return null;

        if (sysDynamic.getDynamicId() != null)
            return sysDynamicMapper.selectById(sysDynamic.getDynamicId());

        sysDynamic.setStartLine(0);
        sysDynamic.setEndLine(1);
        List<SysDynamic> list = sysDynamicMapper.listBy(sysDynamic);
        if (list == null || list.isEmpty())
            return null;

        return list.get(0);
    }

    /**
     * 按照id查询动态表
     * 
     * @param dynamicId
     * @return sysDynamic
     */
    @Override
    public SysDynamic findById(Integer dynamicId)
    {
        logger.debug("debug dynamicId: {}", dynamicId);

        if (dynamicId == null)
            return null;

        return sysDynamicMapper.selectById(dynamicId);
    }

    /**
     * 按照ids查询动态表
     * 
     * @param dynamicIds
     * @return sysDynamic集合
     */
    @Override
    public List<SysDynamic> findByIds(List<Integer> dynamicIds)
    {
        logger.debug("debug dynamicIds: {}", dynamicIds);

        if (dynamicIds == null || dynamicIds.isEmpty())
            return null;

        return sysDynamicMapper.selectByIds(dynamicIds);
    }

    /**
     * 按照条件精确查询动态表返回集合方法
     * 
     * @param sysDynamic
     * @return sysDynamic
     */
    @Override
    public List<SysDynamic> listBy(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return null;

        return sysDynamicMapper.listBy(sysDynamic);
    }

    /**
     * 按照条件模糊查询动态表返回集合方法
     * 
     * @param sysDynamic
     * @return sysDynamic集合
     */
    @Override
    public List<SysDynamic> listLike(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return null;

        return sysDynamicMapper.listLike(sysDynamic);
    }

    /**
     * 按照分页条件精确查询动态表返回集合方法
     * 
     * @param sysDynamic
     * @return sysDynamic集合
     */
    @Override
    public List<SysDynamic> listPageBy(Page page, SysDynamic sysDynamic)
    {
        logger.debug("debug page: {}", page);
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            sysDynamic = new SysDynamic();

        if (page != null)
        {
            sysDynamic.setStartLine(page.getBeginIndex());
            sysDynamic.setEndLine(page.getEveryPage());
        }

        if (sysDynamic.getOrderBy() == null)
            sysDynamic.setOrderBy(SysDynamic.FIELD_DYNAMIC_ADD_TIME + SysDynamic.ORDER_BY_DESC);

        return sysDynamicMapper.listBy(sysDynamic);
    }

    /**
     * 按照分页条件模糊查询动态表返回集合方法
     * 
     * @param sysDynamic
     * @return sysDynamic集合
     */
    @SuppressWarnings("deprecation")
    @Override
    public List<Map<String, Object>> listPageLike(Page page, SysDynamic sysDynamic)
    {
        logger.debug("debug page: {}", page);
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            sysDynamic = new SysDynamic();

        String selectSQL = "";
        selectSQL += " SELECT sd.*,(";
        selectSQL += "     SELECT user_nick_name";
        selectSQL += "     FROM sys_user su";
        selectSQL += "     WHERE su.user_code=sd.user_code";
        selectSQL += " ) AS user_nick_name,(";
        selectSQL += "     SELECT text_content";
        selectSQL += "     FROM sys_text su";
        selectSQL += "     WHERE su.text_code = sd.text_code";
        selectSQL += " ) AS text_content";
        selectSQL += " FROM sys_dynamic sd";

        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("user_code", sysDynamic.getUserCode());
        whereMap.put("dynamic_type", sysDynamic.getDynamicType());
        whereMap.put("dynamic_status", sysDynamic.getDynamicStatus());

        selectSQL = CommonUtils.getSelectSQLWhere(whereMap, true, selectSQL);
        if (StringUtils.isEmpty(sysDynamic.getOrderBy()))
            selectSQL += " ORDER BY dynamic_add_time DESC";

        if (page != null)
            selectSQL += " limit " + page.getBeginIndex() + ", " + page.getEveryPage();

        return sysDynamicMapper.selectCustom(selectSQL);
    }

    /** --公共-业务-修改-- **/
    /**
     * 修改动态表
     * 
     * @param sysDynamic
     * @return boolean
     */
    @Override
    public boolean update(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null || sysDynamic.getDynamicId() == null)
            return false;

        sysDynamicMapper.update(sysDynamic);
        return true;
    }

    /**
     * 批量修改动态表
     * 
     * @param sysDynamics
     * @return boolean
     */
    @Override
    public boolean updateList(List<SysDynamic> sysDynamics)
    {
        logger.debug("debug sysDynamics: {}", sysDynamics);

        if (sysDynamics == null)
            return false;

        List<SysDynamic> news = new ArrayList<SysDynamic>();
        for (SysDynamic sysDynamic : sysDynamics)
            if (sysDynamic != null && sysDynamic.getDynamicId() != null)
                news.add(sysDynamic);

        if (news.isEmpty())
            return false;

        sysDynamicMapper.updateList(news);
        return true;
    }

    /** --公共-业务-删除-- **/
    /**
     * 指定id删除
     * 
     * @param dynamicId
     * @return boolean
     */
    @Override
    public boolean deleteById(Integer dynamicId)
    {
        logger.debug("debug dynamicId: {}", dynamicId);

        if (dynamicId == null)
            return false;

        sysDynamicMapper.deleteById(dynamicId);
        return true;
    }

    /**
     * 批量删除指定ids
     * 
     * @param dynamicId
     * @return boolean
     */
    @Override
    public boolean deleteByIds(List<Integer> dynamicIds)
    {
        logger.debug("debug dynamicIds: {}", dynamicIds);

        if (dynamicIds == null || dynamicIds.isEmpty())
            return false;

        sysDynamicMapper.deleteByIds(dynamicIds);
        return true;
    }

    /**
     * 按照条件精确删除动态表
     * 
     * @param sysDynamic
     * @return boolean
     */
    @Override
    public boolean deleteBy(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return false;

        sysDynamicMapper.deleteBy(sysDynamic);
        return true;
    }

    /**
     * 按照条件模糊删除动态表
     * 
     * @param sysDynamic
     * @return boolean
     */
    @Override
    public boolean deleteLike(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        if (sysDynamic == null)
            return false;

        sysDynamicMapper.deleteLike(sysDynamic);
        return true;
    }

    /** --公共-业务-自定义-- **/
    /**
     * 自定义方法
     * 
     * @param sysDynamic
     * @return map集合
     */
    @Override
    public List<Map<String, Object>> custom(SysDynamic sysDynamic)
    {
        logger.debug("debug sysDynamic: {}", sysDynamic);

        // 自己实现自己的业务
        // sysDynamicMapper.selectCustom(selectSQL);
        return null;
    }

    /** ------------------------------------私有-方法------------------------------------ **/
    /**
     * 过滤参数为空
     * 
     * @param sysDynamics
     * @return sysDynamic集合
     */
    private List<SysDynamic> filterEmpty(List<SysDynamic> sysDynamics)
    {
        List<SysDynamic> news = new ArrayList<SysDynamic>();
        for (SysDynamic sysDynamic : sysDynamics)
            if (sysDynamic != null)
                news.add(sysDynamic);
        return news;
    }
}